/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import bo.Product_BUS;
import com.opensymphony.xwork2.ActionSupport;
import dto.ProductDTO;
import java.sql.SQLException;
import java.util.ArrayList;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts2.interceptor.ServletRequestAware;

/**
 *
 * @author Tam Nguyen
 */
public class ProductAdminAction extends ActionSupport {

    private String ThongBao;
    
    private String product_id;
    private String category_id;
    private String product_name;
    private int unit_price;
    private String descript;
    private double discount;
    private int quantity;
    private String product_status;
    
    private boolean delete = false;
    private boolean update = false;
    private boolean add = false;
    
 

    public void setAdd(boolean add) {
        this.add = true;
    }
    
    public String getThongBao() {
        return ThongBao;
    }

    public void setThongBao(String ThongBao) {
        this.ThongBao = ThongBao;
    }

    
    public void setDelete(boolean delete) {
        this.delete = true;
    }

    public void setUpdate(boolean update) {
        this.update = true;
    }


    public String getProduct_id() {
        return product_id;
    }

    public void setProduct_id(String product_id) {
        this.product_id = product_id;
    }

    public String getCategory_id() {
        return category_id;
    }

    public void setCategory_id(String category_id) {
        this.category_id = category_id;
    }

    public String getProduct_name() {
        return product_name;
    }

    public void setProduct_name(String product_name) {
        this.product_name = product_name;
    }

    public int getUnit_price() {
        return unit_price;
    }

    public void setUnit_price(int unit_price) {
        this.unit_price = unit_price;
    }

    public String getDescript() {
        return descript;
    }

    public void setDescript(String descript) {
        this.descript = descript;
    }

    public double getDiscount() {
        return discount;
    }

    public void setDiscount(double discount) {
        this.discount = discount;
    }

    public int getQuantity() {
        return quantity;
    }

    public void setQuantity(int quantity) {
        this.quantity = quantity;
    }

    public String getProduct_status() {
        return product_status;
    }

    public void setProduct_status(String product_status) {
        this.product_status = product_status;
    }
  
            
    public ProductAdminAction() throws SQLException {
        Product_BUS bus = new Product_BUS();
        list = bus.LoadAllProduct();
    }
    
    private ArrayList<dto.ProductDTO> list = new ArrayList();
    public ArrayList<ProductDTO> getList() {
        return list;
    }

    public void setList(ArrayList<ProductDTO> list) {
        this.list = list;
    }
    
    public String execute() throws Exception  {
        Product_BUS bus = new Product_BUS(); 
        list = bus.LoadAllProduct();
        if(delete == true)
        {
            delete();
            list = bus.LoadAllProduct();
        }
        if(add == true)
        {
            add();
            list = bus.LoadAllProduct();
        }
        if(update == true)
        {
            update();
            list = bus.LoadAllProduct();
        }
        return "success";

    }
    
    public void delete() throws SQLException
    {
        Product_BUS bus = new Product_BUS();
        boolean flag = bus.DeleteProduct(product_id);
        if (flag == true)
        {
            ThongBao = "Delete Completed";
        }
        else
        {
            ThongBao = "Failed to Delete";
        }
    }
    
    public void add() throws SQLException
    {
        ProductDTO pr = new ProductDTO();
        pr.setProduct_id(product_id);
        pr.setCategory_id(category_id);
        pr.setProduct_name(product_name);
        pr.setUnit_price(unit_price);
        pr.setDescript(descript);
        pr.setDiscount(discount);
        pr.setQuantity(quantity);
        pr.setProduct_status(product_status);
        
        Product_BUS bus = new Product_BUS();
        boolean flag = bus.AddProduct(pr);
        if(flag == true)
        {
            ThongBao = "Add a Product completed.";
        }
        else
        {
            ThongBao = "Fail to Add a Customer.";
        }
    }
    
    public void update() throws SQLException
    {
        ProductDTO pr = new ProductDTO();
        pr.setProduct_id(product_id);
        pr.setCategory_id(category_id);
        pr.setProduct_name(product_name);
        pr.setUnit_price(unit_price);
        pr.setDescript(descript);
        pr.setDiscount(discount);
        pr.setQuantity(quantity);
        pr.setProduct_status(product_status);
        
        Product_BUS bus = new Product_BUS();
        boolean flag = bus.UpdateProduct(pr);
        if(flag == true)
        {
            ThongBao = "Update a customer completed.";
        }
        else
        {
            ThongBao = "Fail to update a customer.";
        }
    }
    
    

}
